July Listing 6
         TTL "Listing 6 : IF and FOR Demo"
;
;
;
**********************************
*                                *
*       SPEED/ASM Equates        *
*                                *
**********************************
;
;
;
;
;
;
;
;
*************
* CONSTANTS *
*************
;
;
;
; The following symbols are constants
; for the values "FALSE", "TRUE", and
; Carriage Return (respectively).
;
; These symbols should only appear
; as immediate operands to a 6502
; instruction or in the operand field
; of a pseudo-opcode like BYT
;
;
;
;
;
FALSE    EQU 0
TRUE     EQU 1
CR       EQU $8D
BELL     EQU $87
;
;
;
;
;
;
; "IF" STATEMENT EQUATES
;
; The following symbols should only
; be used in the ADR pseudo-opcode
; following a call to the SPEED/ASM
; IFx routines.
;
;
;
EQ       EQU "="
NE       EQU "#"
GT       EQU ">"
LT       EQU "<"
GE       EQU ">"|"="*256
LE       EQU "<"|"="*256
;
;
;
;
;
;
;
**************************
* SPEED/ASM ENTRY POINTS *
**************************
;
;
;
;
; NOTE: THE EQUATE OF PUTC MUST
; BE CHANGED IF YOU RELOCATE
; SPEED/ASM TO SOME LOCATION
; OTHER THAN $7800
;
;
;
PUTC     EQU $7800
GETC     EQU PUTC+3
SAGL     EQU GETC+3          ;FOR USE BY S/A ONLY- SEE DOC.
SAPC     EQU SAGL+3          ; "   "   "  "   "     "   "
HOME     EQU SAPC+3          ;HOME AND CLEAR
READLN   EQU HOME+3
INIT     EQU READLN+3
FOR      EQU INIT+3
FOR0     EQU FOR+3
NEXT     EQU FOR0+3
IFI      EQU NEXT+3
IFI0     EQU IFI+3
IFS      EQU IFI0+3
IFS0     EQU IFS+3
MOVE     EQU IFS0+3
LOAD     EQU MOVE+3
MOVS     EQU LOAD+3
LDSTR    EQU MOVS+3
PRINT    EQU LDSTR+3
PRTSTR   EQU PRINT+3
PRTINT   EQU PRTSTR+3
RDSTR    EQU PRTINT+3
RDINT    EQU RDSTR+3
ONXGOTO  EQU RDINT+3
CASE     EQU ONXGOTO+3
CASEI    EQU CASE+3
INSET    EQU CASEI+3
NOTINSET EQU INSET+3
ABS      EQU NOTINSET+3
NEG      EQU ABS+3
MUL      EQU NEG+3
DIV      EQU MUL+3
MOD      EQU DIV+3
;
;
;
; Apple DOS Equate:
;
INITDOS  EQU $3EA            ; DOS init routine
CTLD     EQU $84             ; Control-D character
;
;
; Apple Monitor equates
;
EXIT     EQU $FF59
;
;
;
;
;
;
*********************************
*                               *
* IF and FOR demonstration PGM. *
*                               *
* This program creates a text   *
* file on the disk (it's named  *
* "NUMBERS") and writes out a   *
* series of numbers to the disk.*
* The second half of this prog- *
* gram read the data back in    *
* (in various formats) and dis- *
* plays the input info on the   *
* screen.                       *
*                               *
*********************************
;
;
;
;
;
; Initialization section:
;
;
;
SA.PGM.4 JSR INIT            ; Always do this first!
;
;
; The following call activates Apple DOS by
; "reconnecting" the input/output
; hooks at locations $36..$39 (See
; "Beneath Apple DOS" page 5-5).
;
         JSR INITDOS
;
;
; Emulate the BASIC statements:
;
;  PRINT CHR$(4);"NOMON O.I.C"
;
         JSR PRINT
         BYT CR,CTLD,"NOMON O,I,C",CR,0
;
;
;
;
******************************************
;
; File creation section:
;
;
;
; Open the file "NUMBERS" in the typical
; Apple DOS fashion. Note that a
; carriage return is printed first
; in order to insure that the DOS
;  command is recognized.
;
;
         JSR PRINT
         BYT CR,CTLD,"OPEN NUMBERS,S6,D1,L8",CR,0
;
         JSR FOR0
         ADR I,1,10
;
;
; Emulate the BASIC statement:
;
; PRINT CHR$(4);"WRITE NUMBERS,R";I;",B0"
;
         JSR PRINT
         BYT CTLD,"WRITE NUMBERS,R",0
         JSR PRTINT
         ADR I
         JSR PRINT
         BYT ",B0",CR,0
;
;
; Now output the value I to the textfile
;
; This code emulates the BASIC statement:
;
;   PRINT I
;
;
         JSR PRTINT
         ADR I
         LDA #CR             ; Output a CR to finish
         JSR PUTC            ; off the line
;
;
; Repeat for 10 numbers
;
         JSR NEXT
;
;
; At this point a random access file
; has been created with 100 records.
; Each record contains the record
; number of that particular record.
; To insure the integrity of the
; file, it must be closed. The following
; SPEED/ASM code emulates the BASIC
; code:
;
;   PRINT CHR$(4); "CLOSE NUMBERS"
;
         JSR PRINT
         BYT CTLD,"CLOSE NUMBERS",CR,0
;
;
*** End of file creation section.
;
;
;
;
**********************************
;
;
; The following section re-opens
; the text file just created and
; prints the records onto the screen
; This verifies proper operation
; of the file creation process.
;
;
;
         JSR PRINT
         BYT CR,"Verifying Records",CR,CR,0
;
;
         JSR PRINT
         BYT CR,CTLD,"OPEN NUMBERS,L8",CR,0
;
         JSR FOR0
         ADR I,1,10
;
         JSR PRINT
         BYT CTLD,"READ NUMBERS,R",0
         JSR PRTINT
         ADR I
         JSR PRINT
         BYT ",B0",CR,0
;
         JSR READLN          ; Force a new line to be read.
         JSR RDINT
         ADR J
;
; Print the record's value.
;
         JSR PRINT
         BYT "Record number ",0
         JSR PRTINT
         ADR I
         JSR PRINT
         BYT " contained '",0
         JSR PRTINT
         ADR J
         JSR PRINT
         BYT "'",CR,0
;
; Move on to the next record.
;
         JSR NEXT
;
;
; ** End of record verification.
;
;
*********************************
;
; Now print the file out backwards
;
;
;
         JSR PRINT
         BYT CR,"Reading records in reverse order",CR,CR,0
;
         JMP DOFOR           ; Hide the following constants
;
CM1      ADR !-1
C1       ADR 1
C10      ADR 10
;
;
DOFOR    JSR FOR
         ADR I,C10,C1,CM1    ; Stepsize of -1
;
         JSR PRINT
         BYT CTLD,"READ NUMBERS,R",0
         JSR PRTINT
         ADR I
         JSR PRINT
         BYT ",B0",CR,0
;
         JSR READLN          ; Fetch a new line
         JSR RDINT
         ADR J
;
         JSR PRINT
         BYT "Record number ",0
         JSR PRTINT
         ADR I
         JSR PRINT
         BYT " contained '",0
         JSR PRTINT
         ADR J
         JSR PRINT
         BYT "'",CR,0
;
         JSR NEXT
;
;
**********************************
;
;
; Now get the bounds for output
; from the keyboard.
;
         JSR PRINT           ; This turn off the DOS
         BYT CR,CTLD,CR,0    ; READ mode.
;
REREAD0  JSR PRINT
         BYT "Input a lower bounds:",0 ; Get first record #
;
         JSR READLN
         JSR RDINT
         ADR FIRST
         BVS REREAD0
; FIRST must be greater than zero
; and less than or equal to 10.
;
         JSR IFI0
         ADR FIRST,GT,0
         BTR FIRSTOK0
;
         JSR PRINT
         BYT CR,BELL,"Value must be greater than zero",CR,0
         JMP REREAD0
;
FIRSTOK0 JSR IFI0
         ADR FIRST,LE,10
         BTR FIRSTOK1
;
         JSR PRINT
         BYT CR,BELL,"Value must be less than 11",CR,0
         JMP REREAD0
;
;
FIRSTOK1:
REREAD1  JSR PRINT
         BYT CR,"Enter the final value:",0
;
         JSR READLN
         JSR RDINT
         ADR ENDVAL
         BVS REREAD1         ; In case of error
;
; Check to make sure that ENDVAL is
; in the range 1..10.
;
         JSR IFI0
         ADR ENDVAL,GT,0
         BTR ENDOK0
;
         JSR PRINT
         BYT CR,BELL,"Ending value must be greater than zero",CR,0
         JMP REREAD1
;
ENDOK0   JSR IFI0
         ADR ENDVAL,LE,10
         BTR ENDOK1
;
         JSR PRINT
         BYT CR,BELL,"Ending value must be less than 11",0
         JMP REREAD1
;
;
; Now get a stepsize value from the user.
;
ENDOK1:
REREAD2  JSR PRINT
         BYT CR,"Enter a stepsize value:",0
;
         JSR READLN
         JSR RDINT
         ADR STEPSIZE
         BVS REREAD2
;
; The stepsize must be checked to
; make sure that it matches the
; following conditions:
;
; It must be in the range 1..10
;   or   -1..-10.
;
; If ENDVAL is greater than FIRST,
;   STEPSIZE   must be positive.
;
; If ENDVAL is less than FIRST,
;   STEPSIZE   must be negative.
;
;
         JSR IFI0
         ADR STEPSIZE,EQ,0
         BFL SSOK0
;
         JSR PRINT
         BYT CR,BELL,"Stepsize must not equal zero",CR,0
         JMP REREAD2
;
;
SSOK0    JSR IFI0
         ADR STEPSIZE,GT,0
         BFL SSISNEG
;
; STEPSIZE > 0, make sure that ENDVAL >= FIRST.
;
         JSR IFI
         ADR ENDVAL,GE,FIRST
         BTR CHKPOSSS
;
         JSR PRINT
         BYT CR,BELL,"STEPSIZE must be negative",CR,0
         JMP REREAD2
;
; Make sure that STEPSIZE is in the range 1..10.
;
CHKPOSSS JSR IFI0
         ADR STEPSIZE,GE,1
         STA BOOLEAN         ; Save for compound test.
         JSR IFI0
         ADR STEPSIZE,LE,10
         AND BOOLEAN
         BFL BADSS
         JMP GOODSS
;
;
BADSS    JSR PRINT
         BYT CR,BELL,"STEPSIZE must be in the range 1..10",CR,0
         JMP REREAD2
;
;
;
; STEPSIZE < 0, make sure that ENDVAL <= FIRST.
;
SSISNEG  JSR IFI
         ADR ENDVAL,LE,FIRST
         BTR CHKNEGSS
;
         JSR PRINT
         BYT CR,BELL,"STEPSIZE must be positive.",CR,0
         JMP REREAD2
;
;
; Make sure that STEPSIZE is in the range -10..-1.
;
CHKNEGSS JSR IFI0
         ADR STEPSIZE,GE,!-10
         STA BOOLEAN         ; Save for compound test
         JSR IFI0
         ADR STEPSIZE,LE,!-1
         AND BOOLEAN
         BTR GOODSS
;
         JSR PRINT
         BYT CR,BELL,"STEPSIZE must be in the range -1..-10",CR,0
         JMP REREAD2
;
;
; If STEPSIZE contains an appropriate value
; print the records as requested.
;
;
GOODSS   LDA #CR
         JSR PUTC            ; Init for DOS*
;
         JSR FOR
         ADR I,FIRST,ENDVAL,STEPSIZE
;
; Read the specified record
;
         JSR PRINT
         BYT CTLD,"READ NUMBERS,R",0
         JSR PRTINT
         ADR I
         JSR PRINT
         BYT ",B0",CR,0
;
         JSR READLN
         JSR RDINT
         ADR J
;
;
         JSR IFI
         ADR I,EQ,FIRST
         BFL >0
;
         JSR PRINT
         BYT "The first record contains ",0
         JMP PRTREC
;
^0       JSR IFI
         ADR I,EQ,ENDVAL
         BFL >1
;
         JSR PRINT
         BYT "The last record contains ",0
         JMP PRTREC
;
;
^1       JSR IFI0
         ADR I,EQ,2
         BFL >2
;
         JSR PRINT
         BYT "The 2nd record contains ",0
         JMP PRTREC
;
^2       JSR IFI0
         ADR I,EQ,3
         BFL >3
;
         JSR PRINT
         BYT "The 3rd record contains ",0
         JMP PRTREC
;
;
^3       JSR PRINT
         BYT "The ",0
         JSR PRTINT
         ADR I
         JSR PRINT
         BYT "th record contains ",0
;
;
PRTREC   JSR PRINT
         BYT ":  '",0
         JSR PRTINT
         ADR J
         JSR PRINT
         BYT "'",CR,0
;
;
         JSR NEXT
;
;
**********************************
;
;
;  All Done..,
;
         JSR PRINT
         BYT CR,CR,CR,"That's all folks!",CR,0
;
;
;
; Close all open files*
;
         JSR PRINT
         BYT CR,CTLD,"CLOSE",CR,0
;
;
         JMP EXIT
;
;
; Variable declarations
;
;
I        ADR 0
J        ADR 0
FIRST    ADR 0
ENDVAL   ADR 0
STEPSIZE ADR 0
BOOLEAN  BYT 0
;
;
         END